import axios from 'axios';
import { getCookie } from '../utils/cookies';
const http = axios.create({
    baseURL: '/api', //localhost:5173 代理只会处理当前正在运行的开发服务器端口上的请求
    timeout: 5000,  //请求超时时间
});
//添加请求拦截
http.interceptors.request.use(
    config => {
        config.headers.Accept = 'application/json, text/plain, text/html,*/*'

        // 如果本地存放的有cookie 就放入请求头内
        let token = getCookie("briefblog_token");
        if (token) {
            config.headers['briefblog_token'] = token;
        }

        return config
    },
    error => {
        return Promise.reject(error)
    }
)

// 添加响应拦截器
http.interceptors.response.use(
    response => {
        return response.data //这样返回的格式就与apifox测试的数据一致了
    },
    error => {
        return Promise.reject(error)
    }
)

export {
    http,
}